﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace SkillEditor
{
    internal class PortInputIntElement : VisualElement
    {
        private int m_UndoGroup = -1;

        public PortInputIntElement()
        {
            AddField(0, "Int");
        }

        private void AddField(int initialValue, string subLabel)
        {
            var dummy = new VisualElement { name = "dummy" };
            var label = new Label(subLabel);
            dummy.Add(label);
            Add(dummy);
            var field = new IntegerField { value = initialValue };
            var dragger = new FieldMouseDragger<int>(field);
            dragger.SetDragZone(label);
            field.Q("unity-text-input").RegisterCallback<KeyDownEvent>(evt =>
            {
                // Record Undo for input field edit
                //if (m_UndoGroup == -1)
                //{
                //    m_UndoGroup = Undo.GetCurrentGroup();
                //    m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
                //}
                //// Handle scaping input field edit
                //if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1)
                //{
                //    Undo.RevertAllDownToGroup(m_UndoGroup);
                //    m_UndoGroup = -1;
                //    evt.StopPropagation();
                //}
                //// Dont record Undo again until input field is unfocused
                //m_UndoGroup++;
                this.MarkDirtyRepaint();
            });
            // Called after KeyDownEvent
            //field.RegisterValueChangedCallback(evt =>
            //{
            //    // Only true when setting value via FieldMouseDragger
            //    // Undo recorded once per dragger release
            //    if (m_UndoGroup == -1)
            //    {
            //        m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
            //    }
            //    var value = m_Get();
            //    if (value[index] != (float)evt.newValue)
            //    {
            //        value[index] = (float)evt.newValue;
            //        m_Set(value);
            //        m_Node.Dirty(ModificationScope.Node);
            //    }
            //});
            //// Reset UndoGroup when done editing input field & update title
            //field.Q("unity-text-input").RegisterCallback<FocusOutEvent>(evt =>
            //{
            //    m_Node.owner.owner.isDirty = true;
            //    m_UndoGroup = -1;
            //});
            Add(field);
        }
    }
}